home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / SOUND / CRYS270.ZIP / LOADMOD.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-06-26  |  7.8 KB  |  461 lines

  1. ;****************************************************************************
  2. ;*    Ce module permet de charger des fichiers au format MOD
  3. ;*    Comme toutes les fonctions de ce projet on utilise la convention
  4. ;*    d'appel pascal
  5. ;*
  6. ;* Programmé par Sébastien Granjoux
  7. ;* Commencé le 24/12/94
  8. ;* Modification le 02/01/95
  9.  
  10. IDEAL
  11. P386N
  12.  
  13. PUBLIC    LOADMOD
  14.  
  15. INCLUDE "CRYSERR.INC"
  16. INCLUDE "CRYSLOAD.INC"
  17.  
  18. SEGMENT CSEG PARA PUBLIC USE16 'CODE'
  19.  
  20. ASSUME    cs:CSEG
  21.  
  22. MODFLAGPOS    EQU     438h
  23. SILENT        EQU    0
  24.  
  25. AmigaNote    DW  1AC0h,1940h,17D0h,1680h,1530h
  26.         DW  1400h,12E0h,11D0h,10D0h,0FE0h
  27.         DW  0F00h,0E28h,0D60h,0CA0h,0BE8h
  28.         DW  0B40h,0A98h,0A00h,0970h,08E8h
  29.         DW  868h,7F0h,780h,714h,6B0h,650h
  30.         DW  5F4h,5A0h,54Ch,500h,4B8h,474h
  31.         DW  43Ah,3F8h,3C0h,38Ah
  32.         DW  358h,328h,2fah,2d0h,2a6h,280h
  33.         DW  25ch,23ah,21ah,1fch,1e0h,1c5h
  34.         DW  1ach,194h,17dh,168h,153h,140h
  35.         DW  12eh,11dh,10dh,0feh,0f0h,0e2h
  36.         DW  0d6h,0cah,0beh,0b4h,0aah,0a0h
  37.         DW  097h,08fh,087h,07fh,078h,071h
  38.         DW  06Bh,065h,05Fh,05Ah,055h,050h
  39.         DW  04Bh,047h,043h,03Fh,03Ch,038h
  40.         DW  035h,032h,02Fh,02Dh,02Ah,028h
  41.         DW  025h,023h,021h,01Fh,01Eh,01Ch
  42.         DW  000h
  43.  
  44. ;***************************************************************************
  45. ;*    Charge un fichier MOD en mémoire
  46. ;*
  47. ;* Entrée:
  48. ;*    DS:DX    adresse longue du nom du fichier mod
  49. ;*    CS:BX    adresse du driver de fichier utilisé
  50. ;*
  51. ;* Sortie:
  52. ;*    AX    code d'erreur si C=1 et AX différent de zero
  53. ;*
  54.  
  55. PROC    loadmod FAR
  56. LOCAL    nbinst:BYTE,nbmodvoice:BYTE,nulsamp:WORD,nulused:WORD=locvar
  57.  
  58.     push    ds
  59.     push    si
  60.     push    di
  61.     enter    locvar,0
  62.  
  63.     mov    si,SEG Instrument1
  64.     mov    ds,si
  65.     mov    si,OFFSET Instrument1
  66.     mov    cl,MAX_INST
  67. @@voidsample:
  68.     mov    [(INSTRUMENT PTR ds:si).adrseg],0
  69.     add    si,SIZE INSTRUMENT
  70.     dec    cl
  71.     jne    @@voidsample
  72.     mov    [ds:PatternSeg],0
  73.  
  74.     lds    dx,[ss:bp+12]
  75.  
  76.     call    open
  77.     jc      @@error
  78.     mov    bx,ax
  79.  
  80.     xor    cx,cx
  81.     mov    dx,MODFLAGPOS
  82.     call    seek
  83.     jc    @@error
  84.  
  85.     mov    ax,SEG Comments
  86.     mov    ds,ax
  87.     mov    cx,4
  88.     mov    dx,OFFSET Comments
  89.     call    read
  90.     jc    @@error
  91.  
  92.     mov    cx,0FFFFh
  93.     mov    dx,-(MODFLAGPOS+4)
  94.     call    seek
  95.     jc    @@error
  96.  
  97.     mov    [nbmodvoice],4
  98.     mov    [nbinst],15
  99.     cmp    [dword ptr ds:OFFSET Comments],'.K.M'
  100.     je    @@mod31
  101.     cmp    [dword ptr ds:OFFSET Comments],'!K!M'
  102.     je    @@mod31
  103.     cmp    [dword ptr ds:OFFSET Comments],'4TLF'
  104.     je    @@mod31
  105.     cmp    [dword ptr ds:OFFSET Comments],'NHC4'
  106.     je    @@mod31
  107.     cmp    [dword ptr ds:OFFSET Comments],'NHC6'
  108.     je    @@mod6
  109.     cmp    [dword ptr ds:OFFSET Comments],'8TLF'
  110.     je    @@mod8
  111.     cmp    [dword ptr ds:OFFSET Comments],'ATCO'
  112.     je    @@mod8
  113.     cmp    [dword ptr ds:OFFSET Comments],'NHC8'
  114.     jne    @@loadheader
  115. @@mod8:
  116.     add    [nbmodvoice],2
  117. @@mod6:
  118.     add    [nbmodvoice],2
  119. @@mod31:
  120.     add    [nbinst],16
  121. @@loadheader:
  122.  
  123.     mov    cx,20
  124.     mov    dx,OFFSET Comments
  125.     call    read
  126.     jc    @@error
  127.     mov    [byte ptr ds:OFFSET Comments+20],0
  128.  
  129.     mov    cl,[nbinst]
  130.     mov    di,OFFSET Instrument1
  131.     mov    ax,SEG Instrument1
  132.     mov    es,ax
  133.     mov    si,OFFSET Comments+32
  134.  
  135. @@next_inst:
  136.     shl    ecx,16
  137.     mov    cx,30
  138.     mov    dx,si
  139.     call    read
  140.     jc    @@error
  141.  
  142.     mov    al,[ds:si+25]
  143.     cmp    al,64
  144.     jb    @@volume_ok
  145.     mov    al,63
  146. @@volume_ok:
  147.     mov    [(INSTRUMENT PTR es:di).volume],al
  148.  
  149.     mov    ax,TOO_BIG_SAMP
  150.     mov       dx,[ds:si+22]
  151.     rol    dx,8
  152.     cmp    dx,32768-(SAMPLE_BORDER+MIN_REPEAT)/2
  153.     jae    @@error
  154.     add    dx,dx
  155.     mov    [(INSTRUMENT PTR es:di).length],dx
  156.  
  157.     mov    ax,[ds:si+28]
  158.     rol    ax,8
  159.     add    ax,ax
  160.     cmp    ax,2
  161.     ja    @@true_rep
  162.     xor    ax,ax
  163. @@true_rep:
  164.     cmp    dx,ax
  165.     jae    @@lenrepok
  166.     mov    ax,dx
  167. @@lenrepok:
  168.     mov    [(INSTRUMENT PTR es:di).replen],ax
  169.  
  170.     add    di,SIZE INSTRUMENT
  171.     mov    [byte ptr ds:si+22],0
  172.     add    si,32
  173.     shr    ecx,16
  174.     dec    cl
  175.     jne    @@next_inst
  176.  
  177.     mov    ax,SEG Sequence
  178.     mov    ds,ax
  179.     mov    dx,OFFSET Sequence+512-130
  180.     mov    si,dx
  181.     mov    cx,130
  182.     call    read
  183.     jc    @@error
  184.  
  185.     cmp    [nbinst],15
  186.     je    @@no_flag
  187.     xor    cx,cx
  188.     mov    dx,4
  189.     call    seek
  190.     jc    @@error
  191. @@no_flag:
  192.  
  193.     shl    ebx,16
  194.     mov     cl,128 ; pour certain mod [ds:si] n'est pas significatif (PRELUDE)
  195.     mov    di,OFFSET Sequence
  196.     lodsw
  197.     push    ax
  198. @@nb_pattern:
  199.     lodsb
  200.     mul    [nbmodvoice]
  201.     shl    ax,4
  202.     cmp     bx,ax
  203.     jae     @@no_new_pattern
  204.     mov     bx,ax
  205. @@no_new_pattern:
  206.     stosw
  207.     dec     cl
  208.     jne     @@nb_pattern
  209.  
  210.     movzx    ax,[nbmodvoice]
  211.     shl    ax,4
  212.     add    bx,ax
  213.  
  214.     call    allocmem ; reserve de la place pour les patterns
  215.     jc      @@error
  216.     mov    [ds:PatternSeg],ax
  217.  
  218.     pop    cx
  219.     xor    ch,ch
  220.     mov    [ds:LastPos],cx
  221.     add    [ds:LastPos],cx
  222.     mov    si,OFFSET Sequence
  223.     add    [ds:LastPos],si
  224. @@changepat:
  225.     add    [word ptr ds:si],ax
  226.     add    si,2
  227.     dec    cl
  228.     jne    @@changepat
  229.  
  230.     mov    si,ax
  231.     mov    ds,si
  232.     mov    ax,CSEG
  233.     mov    es,ax
  234.  
  235.     mov    cx,bx
  236.     shl    ecx,2+16
  237.     mov    cx,4        ; lecture en 4 fois
  238. @@readpat:
  239.     push    cx
  240.     shr    ebx,16
  241.     shr    ecx,16
  242.     xor    dx,dx
  243.     call    read
  244.     jc    @@error
  245.  
  246.     shl    ebx,16
  247.     mov    ax,ds
  248.     shr    cx,4
  249.     add    ax,cx
  250.     push    ax
  251.     mov    ax,cx
  252.     shl    ecx,16+4
  253.     mov    cx,ax
  254.  
  255.     xor    si,si
  256.     shl    cx,2
  257. @@changenote:
  258.     lodsd
  259.     mov    di,OFFSET AmigaNote+22-24
  260.     rol    ax,8
  261.     mov    bx,ax
  262.     and    bx,0FFFh
  263.     je    @@nonote
  264.     mov    al,0F0h
  265. @@search_oct:
  266.     add    al,10h
  267.     add    di,24
  268.     cmp    bx,[es:di]
  269.     jb    @@search_oct
  270.  
  271.     sub    di,22+2
  272. @@search_note:
  273.     inc    al
  274.     add    di,2
  275.     cmp    bx,[es:di]
  276.     jb    @@search_note
  277.     dec    al
  278. @@nonote:
  279.     mov    [ds:si-4],al
  280.     shr    eax,8
  281.     shr    al,4
  282.     rol    ax,4
  283.     shr    ah,1
  284.     and    ah,0F8h
  285.     ror    eax,8
  286.     call    @@convert_speed
  287.     mov    [ds:si-2],ax
  288.     rol    eax,8
  289.     shl    ax,7
  290.     or    [ds:si-4],al
  291.     and    ah,1Fh
  292.     mov    [ds:si-3],ah
  293.  
  294.     loop    @@changenote
  295.  
  296.     pop    ds
  297.     pop    cx
  298.     dec    cx
  299.     jne    @@readpat
  300.  
  301.  
  302.     shl    ebx,16
  303.     mov    bx,SAMPLE_BORDER+MIN_REPEAT  ;créer un sample nul
  304.     add    bx,15
  305.     shr    bx,4
  306.     call    allocmem
  307.     jc    @@error
  308.     mov    es,ax
  309.     mov    [nulsamp],ax
  310.  
  311.     mov    cx,SAMPLE_BORDER+MIN_REPEAT
  312.     xor    di,di
  313.     mov    ax,SILENT
  314.     rep    stosb
  315.  
  316.     mov    [nulused],0
  317.  
  318.     mov    ax,SEG Instrument1
  319.     mov    es,ax
  320.     mov    di,OFFSET Instrument1
  321.     mov     cl,[nbinst]           ;lecture des samples
  322. @@sample:
  323.     shl     ecx,16
  324.     mov    cx,[(INSTRUMENT PTR es:di).length]
  325.     or    cx,cx
  326.     jne    @@res_mem
  327.  
  328.     mov    ax,[nulsamp]
  329.     mov    [(INSTRUMENT PTR es:di).adrseg],ax
  330.     mov    [(INSTRUMENT PTR es:di).replen],MIN_REPEAT
  331.     mov    [(INSTRUMENT PTR es:di).length],MIN_REPEAT
  332.     mov    [nulused],1
  333.     jmp    @@no_sample
  334. @@res_mem:
  335.     mov    bx,cx
  336.     add    bx,SAMPLE_BORDER+MIN_REPEAT
  337.     add    bx,15
  338.     shr    bx,4
  339.     call    allocmem
  340.     jc    @@error
  341.     mov    [(INSTRUMENT PTR es:di).adrseg],ax
  342.  
  343.     shr    ebx,16
  344.     mov    ds,ax
  345.     xor    ax,ax
  346.     call    read
  347.     jc      @@error
  348.     shl    ebx,16
  349.  
  350. ;    mov    si,[(INSTRUMENT PTR es:di).length]
  351. ;@@unsignedsamp:
  352. ;    add    [byte ptr ds:si-1],128
  353. ;    dec    si
  354. ;    jne    @@unsignedsamp
  355.  
  356.     mov    cx,SAMPLE_BORDER+MIN_REPEAT
  357.     mov    bx,[(INSTRUMENT PTR es:di).length]
  358.     mov    si,bx
  359.     sub    si,[(INSTRUMENT PTR es:di).replen]
  360.  
  361.     cmp    [(INSTRUMENT PTR es:di).replen],2
  362.     ja    @@next_byte
  363.     mov    [(INSTRUMENT PTR es:di).replen],MIN_REPEAT
  364.     mov    [byte ptr ds:si],SILENT
  365.     add    [(INSTRUMENT PTR es:di).length],MIN_REPEAT
  366.     dec    cx
  367.     inc    bx
  368. @@next_byte:
  369.     lodsb
  370.     mov    [ds:bx],al
  371.     inc    bx
  372.     dec    cx
  373.     jne    @@next_byte
  374.  
  375. @@no_sample:
  376.     add     di,SIZE INSTRUMENT
  377.     shr     ecx,16
  378.     dec     cl
  379.     jne     @@sample
  380.  
  381.     shr    ebx,16
  382.     call    close
  383.     jc      @@error
  384.  
  385.     mov    al,[nbmodvoice]
  386.     mov    [es:NbVoice],al
  387.     mov    al,6
  388.     mov    [es:Tempo],al
  389.     mov    al,125
  390.     mov    [es:Bpm],al
  391.  
  392.     cmp    [nulused],0
  393.     jne    @@nul_used
  394.     mov    es,[nulsamp]
  395.     call    freemem
  396. @@nul_used:
  397.     xor    ax,ax
  398.     clc
  399.     jmp    @@ok
  400. @@error:
  401.     stc
  402. @@ok:
  403.     leave
  404.     pop    di
  405.     pop    si
  406.     pop    ds
  407.     ret    4
  408.  
  409. @@convert_speed:
  410.     mov    bl,al
  411.     and    bl,0F8h
  412.     cmp    bl,78h
  413.     jne    @@no_speed
  414.     cmp    ah,1Fh
  415.     jbe    @@no_speed
  416.     add    al,8
  417. @@no_speed:
  418.     retn
  419. ENDP
  420.  
  421. ENDS
  422. END
  423.  
  424. ;***************************************************************************
  425. ;*      Libère la mémoire prise par la fonction loadmod utilise en ds:di
  426. ;*      l'adresse des instruments et en ds:si l'adresse de la partition
  427.  
  428. PROC    Unloadmod FAR
  429.  
  430.     push    ds
  431.  
  432.     mov    ax,SEG PatternSeg
  433.     mov    ds,ax
  434.     mov     ax,[ds:PatternSeg]
  435.     or    ax,ax
  436.     je    @@not_alloc
  437.     mov    es,ax
  438.     call    freemem
  439. @@not_alloc:
  440.  
  441.     mov    bx,OFFSET Instrument1
  442.     mov    cl,MAX_INST
  443. @@sample:
  444.     mov     ax,[(INSTRUMENT PTR ds:bx).adrseg]
  445.     or    ax,ax
  446.     je      @@no_sample
  447.     mov     es,ax
  448.     call    freemem
  449. @@no_sample:
  450.     add     bx,SIZE INSTRUMENT
  451.     dec    cl
  452.     jne    @@sample
  453.  
  454.     pop    ds
  455.  
  456.     ret
  457.  
  458. ENDP
  459.  
  460. ENDS
  461. END